<?php

namespace app\api\controller;
use Qiniu\Auth as Auth;
use Qiniu\Storage\UploadManager;
use think\Controller;

class User extends Controller
{
    /**
     * 用户信息
     */
    public function user_info($header, $data, $user)
    {
        if($user['u_head_img']==''){
            $system=db('system')->find();
            $user['u_head_img']=$system['user_head_img'];
        }
        $user1=array(
            'uid'=>$user['u_id'],//用户id
            'mobile'=>$user['u_mobile'],//用户手机号
            'head_img'=>$user['u_head_img'],//头像
            'certification'=>$user['u_certification'],//认证状态：0未认证；1：已认证；2：申请认证；3：认证失败；
            'integral'=>$user['u_integral'],
        );
        if($user['u_certification']==1 || $user['u_certification']==2){
            $tname=db('store_type')->where(array('st_id'=>$user['u_store_type_id']))->value('st_name');
            $address=explode(',',$user['u_store_region']);
            if($user['u_type']==1){
                $type='老板';
            }else if($user['u_type']==2){
                $type='采购';
            }else if($user['u_type']==3){
                $type='厨师长';
            }else if($user['u_type']==4){
                $type='收银员';
            }else if($user['u_type']==5){
                $type='其他';
            }
            $user1['store_name']=$user['u_store_name'];
            $user1['tid']=$user['u_store_type_id'];
            $user1['tname']=$tname;
            $user1['province']=$address[0];
            $user1['city']=$address[1];
            $user1['place']=$user['u_store_place'];
            $user1['name']=$user['u_real_name'];
            $user1['type_id']=$user['u_type'];
            $user1['type']=$type;
            $user1['u_mobile']=$user['u_real_mobile'];
            $user1['store_img']=$user['u_store_img'];
            $user1['license_img']=$user['u_store_license_img'];
        }else{
            $user1['store_name']='';
            $user1['tid']=0;
            $user1['tname']='';
            $user1['province']='';
            $user1['city']='';
            $user1['place']='';
            $user1['name']='';
            $user1['type_id']='';
            $user1['type']='';
            $user1['u_mobile']='';
            $user1['store_img']='';
            $user1['license_img']='';
        }

        return array('code' => 1, 'msg' => '用户信息查询成功', 'data' => $user1);
    }
    /**
     * 用户剩余积分
     */
    public function user_integral($header, $data, $user)
    {
        $user1=array(
            'integral'=>$user['u_integral'],
        );
        return array('code' => 1, 'msg' => '用户积分查询成功', 'data' => $user1);
    }
    /**
     * 我的收货地址
     */
    public function my_address($header, $data, $user)
    {
        $address = db('user_address')
            ->where(array('ua_status' => 1, 'u_id' => $user['u_id']))
            ->order("ua_default desc,ua_id desc")
            ->limit("0,10")
            ->select();
        $data1 = array();
        foreach ($address as $k => $v) {
            if ($k == 0) {
                $default = 1;
            } else {
                $default = 0;
            }
            $data1[] = array(
                'id' => $v['ua_id'],//收货地址id
                'name' => $v['ua_name'],//收货人名称
                'mobile' => $v['ua_mobile'],//收货人电话
                'province' => $v['ua_province'],//省
                'city' => $v['ua_city'],//市
                'area' => $v['ua_area'],//区
                'nearby' => $v['ua_nearby'],//附近
                'place' => $v['ua_place'],//详细地址
                'default' => $default,//是否为默认地址：1：是默认地址；
            );
        }
        return array('code' => 1, 'msg' => '收货地址查询成功', 'data' => $data1);
    }

    /**
     * 我的搜索词
     */
    public function my_search($header, $data, $user)
    {
        $search = db('search')
            ->where(array('u_id' => $user['u_id']))
            ->find();
        if (empty($search)) {
            $search['s_str'] = '';
        }
        $search1 = explode('|', $search['s_str']);
        //搜索词
        $search1 = array_slice($search1, 0, 20);
        //获取前20个
        $data1['search'] = $search1;

        return array('code' => 1, 'msg' => '我的搜索词', 'data' => $data1);
    }

    /**
     * 删除搜索词
     */
    public function del_search($header, $data, $user)
    {
        $search = db('search')
            ->where(array('u_id' => $user['u_id']))
            ->find();
        if (empty($search)) {
            return array('code' => 1, 'msg' => '删除成功', 'data' => array());
        }
        $res = db('search')
            ->where(array('u_id' => $user['u_id']))
            ->update(array('s_str' => ''));
        if ($res !== false) {
            return array('code' => 1, 'msg' => '删除成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '删除失败', 'data' => array());
        }
    }

    /**
     * 添加收货地址
     */
    public function add_address($header, $data, $user)
    {
        if ($user['u_certification'] == 2) {
            return array('code' => -5, 'msg' => '正在申请实名认证，请认证后操作', 'data' => array());
        }
        if ($user['u_certification'] != 1) {
            return array('code' => -4, 'msg' => '请先实名认证', 'data' => array());
        }
        $check = check_array($data, array('province', 'city', 'area', 'nearby', 'place', 'name', 'mobile'));
        if ($check['code'] != 1) {
            return $check;
        }
        $province = $data['province'];
        $city = $data['city'];
        $area = $data['area'];
        $nearby = $data['nearby'];
        $place = $data['place'];
        $name = $data['name'];
        $mobile = $data['mobile'];
        if (empty($data['default'])) {
            $default = 0;
        } else {
            $default = $data['default'];
        }
        $check = check_format($name, config('regular_str.name'));
        if (!$check) {
            return array('code' => 2, 'msg' => '收货人姓名格式错误', 'data' => array());
        }
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '联系电话格式错误', 'data' => array());
        }
        if ($province == '' || $city == '' || $area == '' || $place == '') {
            return array('code' => 2, 'msg' => '地址选择错误', 'data' => array());
        }
        $address = array(
            'u_id' => $user['u_id'],
            'ua_province' => $province,
            'ua_city' => $city,
            'ua_area' => $area,
            'ua_nearby' => $nearby,
            'ua_place' => $place,
            'ua_name' => $name,
            'ua_mobile' => $mobile,
            'ua_default' => $default,
            'ua_status' => 1,
            'ua_create_time' => time(),
        );
        $res = db('user_address')
            ->insertGetId($address);
        if ($res > 0) {
            if ($default == 1) {
                db('user_address')
                    ->where(array('u_id' => $user['u_id'], 'ua_id' => array('neq', $res)))
                    ->update(array('ua_default' => 0));
            }
            return array('code' => 1, 'msg' => '地址添加成功', 'data' => array('id' => $res));
        } else {
            return array('code' => 2, 'msg' => '地址添加失败', 'data' => array());
        }
    }

    /**
     * 修改收货地址
     */
    public function readdress($header, $data, $user)
    {
        if ($user['u_certification'] == 2) {
            return array('code' => -5, 'msg' => '正在申请实名认证，请认证后操作', 'data' => array());
        }
        if ($user['u_certification'] != 1) {
            return array('code' => -4, 'msg' => '请先实名认证', 'data' => array());
        }
        $check = check_array($data, array('id', 'province', 'city', 'area', 'nearby', 'place', 'name', 'mobile'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id = $data['id'];
        $province = $data['province'];
        $city = $data['city'];
        $area = $data['area'];
        $nearby = $data['nearby'];
        $place = $data['place'];
        $name = $data['name'];
        $mobile = $data['mobile'];
        if (empty($data['default'])) {
            $default = 0;
        } else {
            $default = $data['default'];
        }
        $check = check_format($name, config('regular_str.name'));
        if (!$check) {
            return array('code' => 2, 'msg' => '收货人姓名格式错误', 'data' => array());
        }
        $check = check_format($mobile, config('regular_str.mobile'));
        if (!$check) {
            return array('code' => 2, 'msg' => '联系电话格式错误', 'data' => array());
        }
        if ($province == '' || $city == '' || $area == '' || $place == '') {
            return array('code' => 2, 'msg' => '地址选择错误', 'data' => array());
        }
        $address = db('user_address')
            ->where(array('u_id' => $user['u_id'], 'ua_id' => $id, 'ua_status' => 1))
            ->find();
        if (empty($address)) {
            return array('code' => 2, 'msg' => '没有该收货地址', 'data' => array());
        }
        $address = array(
            'u_id' => $user['u_id'],
            'ua_province' => $province,
            'ua_city' => $city,
            'ua_area' => $area,
            'ua_nearby' => $nearby,
            'ua_place' => $place,
            'ua_name' => $name,
            'ua_mobile' => $mobile,
            'ua_default' => $default,
            'ua_status' => 1,
            'ua_update_time' => time(),
        );
        $res = db('user_address')
            ->where(array('ua_id' => $id))
            ->update($address);
        if ($res !== false) {
            if ($default == 1) {
                db('user_address')
                    ->where(array('u_id' => $user['u_id'], 'ua_id' => array('neq', $id)))
                    ->update(array('ua_default' => 0));
            }
            return array('code' => 1, 'msg' => '地址修改成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '地址修改失败', 'data' => array());
        }
    }

    /**
     * 默认收货地址
     */
    public function default_address($header, $data, $user)
    {
        if ($user['u_certification'] == 2) {
            return array('code' => -5, 'msg' => '正在申请实名认证，请认证后操作', 'data' => array());
        }
        if ($user['u_certification'] != 1) {
            return array('code' => -4, 'msg' => '请先实名认证', 'data' => array());
        }
        $check = check_array($data, array('id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id = $data['id'];
        $address = db('user_address')
            ->where(array('u_id' => $user['u_id'], 'ua_id' => $id, 'ua_status' => 1))
            ->find();
        if (empty($address)) {
            return array('code' => 2, 'msg' => '没有该收货地址', 'data' => array());
        }
        if ($address['ua_default'] == 1) {
            return array('code' => 2, 'msg' => '改地址已经是默认地址了', 'data' => array());
        }
        $res = db('user_address')
            ->where(array('ua_id' => $id))
            ->update(array('ua_default' => 1));
        if ($res !== false) {
            db('user_address')
                ->where(array('u_id' => $user['u_id'], 'ua_id' => array('neq', $id)))
                ->update(array('ua_default' => 0));
            return array('code' => 1, 'msg' => '默认地址成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '默认地址失败', 'data' => array());
        }
    }

    /**
     * 删除收货地址
     */
    public function del_address($header, $data, $user)
    {
        if ($user['u_certification'] == 2) {
            return array('code' => -5, 'msg' => '正在申请实名认证，请认证后操作', 'data' => array());
        }
        if ($user['u_certification'] != 1) {
            return array('code' => -4, 'msg' => '请先实名认证', 'data' => array());
        }
        $check = check_array($data, array('id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $id = $data['id'];
        $address = db('user_address')
            ->where(array('u_id' => $user['u_id'], 'ua_id' => $id, 'ua_status' => 1))
            ->find();
        if (empty($address)) {
            return array('code' => 2, 'msg' => '没有该收货地址', 'data' => array());
        }
        $res = db('user_address')
            ->where(array('ua_id' => $id))
            ->update(array('ua_status' => 2, 'ua_end_time' => time()));
        if ($res !== false) {
            return array('code' => 1, 'msg' => '删除地址成功', 'data' => array());
        } else {
            return array('code' => 2, 'msg' => '删除地址失败', 'data' => array());
        }
    }

    /**
     * 我的购物车
     */
    public function my_car($header, $data, $user)
    {

        $car = db('goods_cart a')
            ->field("a.gc_id,a.g_id,a.gn_id,a.g_number,b.g_name,b.g_img,c.gn_name,c.gn_weight,c.gn_price,c.gn_one_number,c.gn_stock,c.gn_old_price,c.gn_details_number,c.gn_details_unit")
            ->join("fr_goods b", "a.g_id = b.g_id", "left")
            ->join("fr_goods_norm c", "a.gn_id = c.gn_id", "left")
            ->where(array('a.u_id' => $user['u_id'], 'a.gc_status' => 1))
            //查询未操作数据
//            ->where(array('u_id'=>$user['u_id'],'gc_status'=>1,'b.g_status'=>1,'c.gn_status'=>1,'c.gn_stock'=>array('gt',0)))
            //查询条件数据（商品状态、规格状态、库存大于0）
            ->order("a.gc_id desc")
            ->select();
        $car1=array();
        $all_price1=0;
        foreach ($car as $k => $v) {
            $weight_price = round($v['gn_price'] / ($v['gn_weight'] * 2), 2);
            $all_price=$v['gn_price']*$v['g_number'];
            $all_price1+=$all_price;
            $car1[] = array(
                'gc_id' => $v['gc_id'],
                'g_id' => $v['g_id'],
                'gn_id' => $v['gn_id'],
                'name'=>$v['g_name'],
                'number'=>$v['g_number'],
                'all_price'=>$all_price,
                'img_url'=>$v['g_img'],
                'gn_name'=>$v['gn_name'],
                'gn_name'=>$v['gn_name'],
                'weight' => $v['gn_weight'],//总重量
                'describe' => $v['gn_one_number'],//单件描述
                'price' => $v['gn_price'],//售价
                'old_price' => $v['gn_old_price'],//原价
                'weight_price' => $weight_price,//一斤价格
                'stock' => $v['gn_stock'],//库存
                'detail_price' => round($v['gn_price'] / ($v['gn_details_number']), 2).'/'.$v['gn_details_unit'],//详细价格
            );
        }
        return array('code' => 1, 'msg' => '我的购物车', 'data' => $car1);
    }
    /**
     * 兑换优惠券
     */
    public function exchange_coupon($header, $data, $user)
    {
        $check = check_array($data, array('c_id'));
        if ($check['code'] != 1) {
            return $check;
        }
        $c_id=$data['c_id'];
        $coupon=db('coupon')
            ->where(array('c_id'=>$c_id,'c_status'=>1,'c_type'=>2,'c_end_time'=>array('gt',time()+24*3600),'c_start_time'=>array('lt',time())))
            ->find();
        //对应优惠券
        if(empty($coupon)){
            return array('code' => 2, 'msg' => '优惠券出错无法兑换', 'data' => array());
        }
        if($user['u_integral']<$coupon['c_need_integral']){
            return array('code' => 2, 'msg' => '您的积分不足，下单可领取积分', 'data' => array());
        }
        db()->startTrans();
        $data1=array('u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'uc_discount'=>$coupon['c_discount'],'uc_min_buy'=>$coupon['c_min_buy'],'uc_create_time'=>time(),'uc_start_time'=>$coupon['c_start_time'],'uc_end_time'=>$coupon['c_end_time'],'uc_type'=>2,'uc_status'=>1);
        $res=db('user_coupon')
            ->insertGetId($data1);
        //添加用户优惠券
        $data2=array('ucl_create_time'=>time(),'uc_id'=>$res,'u_id'=>$user['u_id'],'c_id'=>$coupon['c_id'],'c_num'=>1,'c_type'=>2);
        $res1=db('user_coupon_log')
            ->insertGetId($data2);
        //增加优惠卷记录
        $res2=db('coupon')
            ->where(array('c_id'=>$c_id,'c_status'=>1))
            ->setInc('u_grant_number');
        //增加优惠券兑换量
        $res3=db('user')
            ->where(array('u_id'=>$user['u_id']))
            ->setDec('u_integral',$coupon['c_need_integral']);
        //减少用户积分
        $old_integral=$user['u_integral'];
        $new_integral= $old_integral-$coupon['c_need_integral'];
        $data3=array('u_id'=>$user['u_id'],'old_integral'=>$old_integral,'new_integral'=>$new_integral,'ui_integral'=>$coupon['c_need_integral'],'ui_status'=>2,'ui_type'=>2,'ui_create_time'=>time(),'c_id'=>$c_id,'uc_id'=>$res);
        $res4=db('user_integral')->insertGetId($data3);
        //用户积分记录
        if($res > 0 && $res1 > 0 && $res2 !== false && $res3 !== false && $res4 > 0){
            db()->commit();
            return array('code' => 1, 'msg' => '已兑换成功，可在我的优惠价进行查看', 'data' => array());
        }else{
            db()->rollback();
            return array('code' => 2, 'msg' => '兑换失败', 'data' => array());
        }
    }
    /**
     * 上传图片的信息
     */
    public function upload_information($header, $data, $user)
    {
        $qiniu_config = config('qiniu');
        $accessKey = $qiniu_config['ACCESSKEY'];
        $secretKey = $qiniu_config['SECRETKEY'];
        $auth  = new Auth($accessKey, $secretKey);
        $token = $auth->uploadToken($qiniu_config['bucket']);
        return array('code' => 1, 'msg' => '上传图片获取信息', 'data' => array('token'=>$token,'url'=>config('qiniu.domain')));
    }
    /**
     * 修改头像
     */
    public function head_img($header, $data, $user)
    {
        $head_img=$data['head_img'];
        $res=db('user')
            ->where(array('u_id'=>$user['u_id']))
            ->update(array('u_head_img'=>$head_img));
        if($res!==false){
            return array('code' => 1, 'msg' => '修改成功', 'data' => array());
        }else{
            return array('code' => 2, 'msg' => '修改失败', 'data' => array());
        }
    }
    /**
     * 我的优惠券
     */
    public function my_coupon($header, $data, $user)
    {
        $check = check_array($data, array('page','size','type'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        $where['a.u_id']=$user['u_id'];
        if($data['type']==1){
            $where['a.uc_status']=1;
            $where['a.uc_end_time']=array('gt',time());
        }else if($data['type']==2){
            $where['a.uc_status']=array('neq',1);
        }else if($data['type']==3){
            $where['a.uc_status']=1;
            $where['a.uc_end_time']=array('lt',time());
        }else{
            return array('code' => -3, 'msg' => '请传入正确的状态', 'data' => array());
        }
        $coupon=db('user_coupon a')
            ->field("a.*,b.c_name")
            ->join("fr_coupon b","a.c_id = b.c_id")
            ->where($where)
            ->order("a.uc_id desc")
            ->limit("$now,$size")
            ->select();
        $coupon1=array();
        foreach ($coupon as $v){
            $coupon1[]=array(
                'uc_id'=>$v['uc_id'],
                'name'=>$v['c_name'],
                'discount'=>$v['uc_discount'],
                'min_buy'=>$v['uc_min_buy'],
                'start_day'=>date('Y.m.d',$v['uc_start_time']),
                'end_day'=>date('Y.m.d',$v['uc_end_time']),
                'type'=>$data['type'],
                'obtain'=>$v['uc_type'],
            );
        }
        $count=db('user_coupon a')
            ->where($where)
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '我的优惠券', 'data' => array('data'=>$coupon1,'max_page'=>$max_page,'type'=>$data['type']));
    }
    /**
     * 我的积分记录
     */
    public function integral_log($header, $data, $user)
    {
        $check = check_array($data, array('page','size'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        $where['a.u_id']=$user['u_id'];
        $log=db('user_integral a')
            ->field("a.*,b.o_real_price")
            ->join("fr_order b","a.o_id = b.o_id","left")
            ->where($where)
            ->order("a.ui_id desc")
            ->limit("$now,$size")
            ->select();
        $log1=array();
        foreach($log as $k=>$v){
            if($v['o_real_price']<=0){
                $v['o_real_price']=0;
            }
            $log1[]=array("id"=>$v['ui_id'],'day'=>date('Y-m-d',$v['ui_create_time']),'status'=>$v['ui_status'],'integral'=>$v['ui_integral'],'type'=>$v['ui_type'],'order_num'=>$v['o_order_num'],'price'=>$v['o_real_price']);
        }
        $count=db('user_integral a')
            ->join("fr_order b","a.o_id = b.o_id","left")
            ->where($where)
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '积分记录', 'data' => array('data'=>$log1,'max_page'=>$max_page));
    }
    /**
     * 购物车数量
     */
    public function car_number($header, $data, $user)
    {
        $car=db("goods_cart")
            ->field("count(g_id) as goods_number,sum(g_number) as all_number")
            ->where(array("u_id"=>$user['u_id'],'gc_status'=>1))
            ->group("u_id")
            ->find();
        if(!isset($car['goods_number']) || $car['goods_number']<=0){
            $car['goods_number']=0;
        }else{
            $car['goods_number']=(int)$car['goods_number'];
        }
        if(!isset($car['all_number']) || $car['all_number']<=0){
            $car['all_number']=0;
        }else{
            $car['all_number']=(int)$car['all_number'];
        }
        return array('code' => 1, 'msg' => '购物车', 'data' => $car);
    }
    /**
     * 购物车数量
     */
    public function useful_coupon($header, $data, $user)
    {
        $check = check_array($data, array('ids'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        $ids=$data['ids'];
        if($ids==''){
            return array('code' => 2, 'msg' => '请选择购物车商品', 'data' => array());
        }
        $car=db("goods_cart a")
            ->field("a.*,b.g_name,b.g_status,c.gn_price,c.gn_stock,c.gn_status")
            ->join("fr_goods b","a.g_id = b.g_id","left")
            ->join("fr_goods_norm c","a.gn_id = c.gn_id","left")
            ->where(array("a.gc_id"=>array('in',$ids),'u_id'=>$user['u_id']))
            ->select();
        if(empty($car)){
            return array('code' => 2, 'msg' => '请选择购物车商品', 'data' => array());
        }
        $all_price=0;
        foreach($car as $v){
            if($v['gc_status']!=1){
                return array('code' => 2, 'msg' => '购物车错误', 'data' => array());
            }
            if($v['g_status']!=1){
                return array('code' => 2, 'msg' => $v['g_name'].'-暂时停售', 'data' => array());
            }
            if($v['gn_status']!=1){
                return array('code' => 2, 'msg' => $v['g_name'].'-有规格暂时停售', 'data' => array());
            }
            if($v['g_number']<=0){
                return array('code' => 2, 'msg' => '购买商品数量必须大于1', 'data' => array());
            }
            if($v['g_number']>$v['gn_stock']){
                return array('code' => 2, 'msg' => $v['g_name'].'-库存不足', 'data' => array());
            }
            $all_price+=$v['g_number']*$v['gn_price'];
        }
//        $system=db('system')
//            ->where(array('id'=>1))
//            ->find();
//        if($system['exempt_freight_price']>0 && $all_price>=$system['exempt_freight_price']){
//            $freight=0;
//        }else{
//            $freight=$system['freight'];
//        }
        $coupon=db('user_coupon a')
            ->field("a.*,b.c_name")
            ->join("fr_coupon b","a.c_id = b.c_id")
            ->where(array('a.uc_status'=>1,"a.uc_end_time"=>array('gt',time()),'a.uc_start_time'=>array('elt',time()),'a.u_id'=>$user['u_id'],'a.uc_min_buy'=>array("elt",$all_price)))
            ->order("a.uc_discount desc,a.uc_end_time")
            ->select();
        $coupon1=array();
        foreach ($coupon as $v){
            $coupon1[]=array(
                'uc_id'=>$v['uc_id'],
                'name'=>$v['c_name'],
                'discount'=>$v['uc_discount'],
                'min_buy'=>$v['uc_min_buy'],
                'start_day'=>date('Y.m.d',$v['uc_start_time']),
                'end_day'=>date('Y.m.d',$v['uc_end_time']),
                'type'=>1,
                'obtain'=>$v['uc_type'],
            );
        }
        $data1=$coupon1;
        return array('code' => 1, 'msg' => '可用优惠券', 'data' => $data1);
    }
    /**
     * 提交需求
     */
    public function demand($header, $data, $user)
    {
        $check = check_array($data, array('gt_id','name','img'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        $type=db('goods_type')
            ->where(array('gt_id'=>$data['gt_id'],'gt_status'=>1))
//            ,'gt_type'=>3
            ->find();
        if(empty($type)){
            return array('code' => 0, 'msg' => '没有该分类', 'data' => array());
        }
        if(trim($data['name'])==''){
            return array('code' => 0, 'msg' => '请输入商品名称', 'data' => array());
        }
        $res=db('goods_demand')
            ->insertGetId(array('u_id'=>$user['u_id'],'gt_id'=>$data['gt_id'],'gd_name'=>trim($data['name']),'gd_img'=>$data['img'],'gd_create_time'=>time(),'gd_status'=>1));
        if($res>0){
            return array('code' => 1, 'msg' => '提交成功', 'data' => array());
        }else{
            return array('code' => 0, 'msg' => '提交失败', 'data' => array());
        }
    }
    /**
     * 需求列表
     */
    public function demand_list($header, $data, $user)
    {
        $check = check_array($data, array('page','size'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        if (empty($data['page']) || $data['page'] <= 1) {
            $data['page'] = 1;
        }
        if (empty($data['size']) || $data['size'] < 1) {
            $data['size'] = 10;
        }
        $page = $data['page'];
        $size = $data['size'];
        $now = ($page - 1) * $size;
        $demand=db('goods_demand')
            ->where(array('u_id'=>$user['u_id']))
            ->order("gd_id desc")
            ->limit("$now,$size")
            ->select();
        $demand1=array();
        foreach($demand as $v){
            $demand1[]=array('gd_id'=>$v['gd_id'],'name'=>$v['gd_name'],'status'=>$v['gd_status']);
        }
        $count = db('goods_demand')
            ->where(array('u_id'=>$user['u_id']))
            ->count();
        $max_page=ceil($count/$size);
        return array('code' => 1, 'msg' => '需求列表', 'data' => array('data'=>$demand1,'max_page'=>$max_page));
    }
    /**
     * 需求详情
     */
    public function demand_details($header, $data, $user)
    {
        $check = check_array($data, array('gd_id'));
        if ($check['code'] != 1) {
            return $check;
        }
        //以上是检测传输数据
        $demand=db('goods_demand a')
            ->field("a.*,b.gt_name")
            ->join("fr_goods_type b","a.gt_id = b.gt_id","left")
            ->where(array('a.u_id'=>$user['u_id'],'a.gd_id'=>$data['gd_id']))
            ->find();
        $img=explode(',', $demand['gd_img']);
        if(empty($img) || count($img)<=0 || $img[0]==''){
            $img=array();
        }
        $demand1=array('gd_id'=>$demand['gd_id'],'name'=>$demand['gd_name'],'status'=>$demand['gd_status'],'gt_name'=>$demand['gt_name'],'day'=>date('Y-m-d',$demand['gd_create_time']),'img'=>$img);
        return array('code' => 1, 'msg' => '需求列表', 'data' => $demand1);
    }
    /**
     * 商铺类型
     */
    public function store_type($header, $data, $user)
    {
        $type=db("store_type")
            ->where(array('st_status'=>1))
            ->order("st_sort desc")
            ->select();
        $type1=array();
        foreach($type as $v){
            $type1[]=array('tid'=>$v['st_id'],'name'=>$v['st_name']);
        }
        return array('code' => 1, 'msg' => '商铺类型', 'data' => $type1);
    }
    /**
     * 申请实名认证
     */
    public function identity($header, $data, $user)
    {
        $check = check_array($data, array('store_name','tid','province', 'city','place','name','type_id','mobile','store_img','license_img'));
//        'area', 'nearby',
        if ($check['code'] != 1) {
            return $check;
        }
        if($user['u_certification']!=0 && $user['u_certification']!=3){
            return array('code' => 2, 'msg' => '已认证或正在认证，无法再次申请', 'data' => array());
        }
        if($data['store_name']==''){
            return array('code' => 2, 'msg' => '请输入店铺名称', 'data' => array());
        }
        $type=db("store_type")
            ->where(array('st_status'=>1,'st_id'=>$data['tid']))
            ->find();
        if(empty($type)){
            return array('code' => 2, 'msg' => '请选择正确的店铺类型', 'data' => array());
        }
        if($data['province']=='' || $data['city']==''){
//            || $data['area']=='' || $data['nearby']==''
            return array('code' => 2, 'msg' => '请选择正确的区域', 'data' => array());
        }
        $data['area']='';
        $data['nearby']='';
        if($data['place']==''){
            return array('code' => 2, 'msg' => '请输入详细地址', 'data' => array());
        }
        $check=check_format($data['name'],config('regular_str.name'));
        if(!$check){
            return array('code' => 2, 'msg' => '请输入正确的收货人', 'data' => array());
        }
        $check=check_format($data['mobile'],config('regular_str.mobile'));
        if(!$check){
            return array('code' => 2, 'msg' => '请输入正确的手机号', 'data' => array());
        }
        if($data['type_id']>5 || $data['type_id']<1){
            return array('code' => 2, 'msg' => '请选择正确的身份', 'data' => array());
        }
        if($data['store_img']==''){
            return array('code' => 2, 'msg' => '请上传店铺图片', 'data' => array());
        }
        if($data['license_img']==''){
            return array('code' => 2, 'msg' => '请上传营业执照', 'data' => array());
        }
        $system=db('system')
            ->where(array('id'=>1))
            ->find();
        $data1=array('u_real_name'=>$data['name'],'u_real_mobile'=>$data['mobile'],'u_type'=>$data['type_id'],'u_certification'=>2,'u_certification_time'=>time(),'u_store_name'=>$data['store_name'],'u_store_type_id'=>$data['tid'],'u_store_img'=>$data['store_img'],'u_store_license_img'=>$data['license_img'],'u_store_region'=>$data['province'].','.$data['city'].','.$data['area'].','.$data['nearby'],'u_store_place'=>$data['place']);
        $res1=db('user')
            ->where(array('u_id'=>$user['u_id']))
            ->update($data1);
        if($res1!==false){
            if(iconv_strlen($data['store_name'],"UTF-8")>=$system['store_name_len'] && iconv_strlen($data['place'],"UTF-8")>$system['place_len'] && $system['store_name_len']>0 && $system['place_len']>0){
                $res=db('user')
                    ->where(array('u_id'=>$user['u_id']))
                    ->update(array('u_certification'=>1,'u_store_fail_reason'=>'直接审核通过'));
//                $address=array('u_id'=>$user['u_id'],'ua_province'=>$data['province'],'ua_city'=>$data['city'],'ua_area'=>$data['area'],'ua_nearby'=>$data['nearby'],'ua_place'=>$data['place'],'ua_name'=>$data['name'],'ua_mobile'=>$data['mobile'],'ua_default'=>1,'ua_status'=>1,'ua_create_time'=>time());
//                $res2=db("user_address")
//                    ->insertGetId($address);
//                $res3=db('user_address')
//                    ->where(array('u_id'=>$user['u_id'],'ua_id'=>array('neq',$res2),'ua_status'=>1))
//                    ->update(array('ua_default'=>0));
            }
            return array('code' => 1, 'msg' => '提交成功', 'data' => array());
        }else{
            return array('code' => 2, 'msg' => '提交失败', 'data' => array());
        }
    }

}